//
//  BKLoadingViewController.m
//  I'm BKer
//
//  Created by Nguyen Van Hoa on 5/14/13.
//  Copyright (c) 2013 vanhoavn. All rights reserved.
//

#import "BKLoadingViewController.h"

@interface BKLoadingViewController ()

@end

static BKLoadingViewController* mInst = NULL;

@implementation BKLoadingViewController
@synthesize mButton,mLoading;
- (id)init
{
    return [self initWithNibName:@"BKLoadingViewController" bundle:nil];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)reload
{
    mLoading.hidden=NO;
    mButton.hidden=YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^(){
        if([BKStudentManager loadInfo]){
            dispatch_async(dispatch_get_main_queue(), ^(){
                [mInst dismissModalViewControllerAnimated:NO];
            });
        }else{
            mLoading.hidden=YES;
            mButton.hidden=NO;
        }
    });
}
-(void)reloadLocal
{
    mLoading.hidden=NO;
    mButton.hidden=YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^(){
        if([BKStudentManager loadInfoLocalFirst]){
            dispatch_async(dispatch_get_main_queue(), ^(){
                [mInst dismissModalViewControllerAnimated:NO];
            });
        }else{
            mLoading.hidden=YES;
            mButton.hidden=NO;
        }
    });
}
+(void)presentInViewControllerAndReadingFromLocal:(UIViewController *)parent
{
    dispatch_async(dispatch_get_main_queue(), ^(){
        if(mInst==NULL) mInst=[[BKLoadingViewController alloc] init];
        [parent presentModalViewController:mInst animated:NO];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [mInst reloadLocal];
        });
    });
}
+(void)presentInViewController: (UIViewController*)parent
{
    dispatch_async(dispatch_get_main_queue(), ^(){
        if(mInst==NULL) mInst=[[BKLoadingViewController alloc] init];
        [parent presentModalViewController:mInst animated:NO];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [mInst reload];
        });
    });
};

@end
